﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace lcs.DAL
{
	/// <summary>
	/// 数据访问类:materialinfo
	/// </summary>
	public partial class materialinfo
	{
		public materialinfo()
		{}
        public DataSet GetMaterial(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select distinct(material) from materialinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet GetListname(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select distinct(supplier) from materialinfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        
        public int Getdid(int mid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select did FROM materialinfo where mid=" + mid);
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return int.Parse(obj.ToString());
            }
        }
        //获取是否有物料员
        public bool Getin(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT count(1) ");
            strSql.Append("FROM  dbo.user_project LEFT OUTER JOIN dbo.[user] ON dbo.user_project.user_id = dbo.[user].user_id RIGHT OUTER JOIN  dbo.materialinfo ON dbo.user_project.materialinfo_proid = dbo.materialinfo.proid WHERE  dbo.materialinfo.mid=@mid and dbo.[user].user_name is not null");
            SqlParameter[] parameters = {
					new SqlParameter("@mid", SqlDbType.Int,4)
			};
            parameters[0].Value =id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);       
           
        }
        public bool setproid(int uid,string mid)
        {
            string sql = "select distinct [value] = stuff((select ','+ proid from (select distinct [proid] from materialinfo where mid in("+mid+") ) as a for xml path('')) , 1 , 1 , '') from materialinfo";
            string sub="";
            sub= DbHelperSQL.GetSingle(sql.ToString()).ToString();
            if (sub != "")
            {
                bool r = false;
                string[] s=sub.Split(',');
                foreach (string a in s)
                {
                    sql =  " materialinfo_proid='" + a+"'";
                    user_project u = new user_project();
                    if (u.GetRecordCount(sql) == 0)
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into user_project(");
                        strSql.Append("user_id,materialinfo_proid,materialinfo_mid)");
                        strSql.Append(" values (");
                        strSql.Append("@user_id,@materialinfo_proid,@materialinfo_mid)");
                        strSql.Append(";select @@IDENTITY");
                        SqlParameter[] parameters = {
					new SqlParameter("@user_id", SqlDbType.Int,4),
					new SqlParameter("@materialinfo_proid", SqlDbType.NVarChar,50),
					new SqlParameter("@materialinfo_mid", SqlDbType.Int,4)};
                        parameters[0].Value = uid;
                        parameters[1].Value = a;
                        object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
                        if (obj == null)
                        {
                            r = false;
                        }
                    }
                    else
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update user_project ");
                        strSql.Append(" set user_id=@user_id where materialinfo_proid=@materialinfo_proid");
                        SqlParameter[] parameters = {
					new SqlParameter("@user_id", SqlDbType.Int,4),
					new SqlParameter("@materialinfo_proid", SqlDbType.NVarChar,50)};					
                        parameters[0].Value = uid;
                        parameters[1].Value = a;
                        int i = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                        if (i==0)
                        {
                            r = false;
                        }
                    }
                }
                return r;
            }
            return false;
        }
        public bool Updateuid(int uid, string mid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update materialinfo set ");
            strSql.Append("uid=@uid ");
            strSql.Append(" where mid in ("+mid+")");
            SqlParameter[] parameters = {									
					new SqlParameter("@uid", SqlDbType.Int,4)};
            parameters[0].Value = uid;            
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool Updatestatus(int mid,int status)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update materialinfo set ");           
            strSql.Append("mstatus=@mstatus ");           
            strSql.Append(" where mid=@mid");
            SqlParameter[] parameters = {					
					new SqlParameter("@mstatus", SqlDbType.Int,4),					
					new SqlParameter("@mid", SqlDbType.Int,4)};
            parameters[0].Value = status;
            parameters[1].Value = mid;           

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public float GetNum(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select nums FROM Vmaterialnums where mid="+id);            
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToSingle(obj);
            }
        }
        public string  GetRemark(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select remark FROM Vmaterialin where mid=" + id);
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return "";
            }
            else
            {
                return obj.ToString();
            }
        }
        public int GetsupplyPlanId(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select supplyPlanId FROM Vmaterialin where mid=" + id);
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetDistinctProidList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select distinct(proid),projectName,cguid from materialinfo ");
             if (strWhere.Trim() != "")
            {
                strSql.Append(" where "+strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("mid", "materialinfo"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int mid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from materialinfo");
			strSql.Append(" where mid=@mid");
			SqlParameter[] parameters = {
					new SqlParameter("@mid", SqlDbType.Int,4)
			};
			parameters[0].Value = mid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(lcs.Model.materialinfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into materialinfo(");
            strSql.Append("proid,usework,projectname,projecttype,supplier,material,nums,unit,suppliername,supplierphone,mstatus,uid,mtype,mbm,mmodel,did,cguid,unitPrice)");
            strSql.Append(" values (");
            strSql.Append("@proid,@usework,@projectname,@projecttype,@supplier,@material,@nums,@unit,@suppliername,@supplierphone,@mstatus,@uid,@mtype,@mbm,@mmodel,@did,@cguid,@unitPrice)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@proid", SqlDbType.NVarChar,50),
					new SqlParameter("@usework", SqlDbType.NVarChar,50),
					new SqlParameter("@projectname", SqlDbType.NVarChar,50),
					new SqlParameter("@projecttype", SqlDbType.NVarChar,50),
					new SqlParameter("@supplier", SqlDbType.NVarChar,50),
					new SqlParameter("@material", SqlDbType.NVarChar,250),
					new SqlParameter("@nums", SqlDbType.Decimal,9),
					new SqlParameter("@unit", SqlDbType.NChar,10),
					new SqlParameter("@suppliername", SqlDbType.NVarChar,50),
					new SqlParameter("@supplierphone", SqlDbType.NVarChar,150),
					new SqlParameter("@mstatus", SqlDbType.Int,4),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@mtype", SqlDbType.Int,4),
					new SqlParameter("@mbm", SqlDbType.NVarChar,50),
					new SqlParameter("@mmodel", SqlDbType.NVarChar,200),
					new SqlParameter("@did", SqlDbType.Int,4),
					new SqlParameter("@cguid", SqlDbType.Int,4),
					new SqlParameter("@unitPrice", SqlDbType.Money,8)};
            parameters[0].Value = model.proid;
            parameters[1].Value = model.usework;
            parameters[2].Value = model.projectname;
            parameters[3].Value = model.projecttype;
            parameters[4].Value = model.supplier;
            parameters[5].Value = model.material;
            parameters[6].Value = model.nums;
            parameters[7].Value = model.unit;
            parameters[8].Value = model.suppliername;
            parameters[9].Value = model.supplierphone;
            parameters[10].Value = model.mstatus;
            parameters[11].Value = model.uid;
            parameters[12].Value = model.mtype;
            parameters[13].Value = model.mbm;
            parameters[14].Value = model.mmodel;
            parameters[15].Value = model.did;
            parameters[16].Value = model.cguid;
            parameters[17].Value = model.unitPrice;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(lcs.Model.materialinfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update materialinfo set ");
            strSql.Append("proid=@proid,");
            strSql.Append("usework=@usework,");
            strSql.Append("projectname=@projectname,");
            strSql.Append("projecttype=@projecttype,");
            strSql.Append("supplier=@supplier,");
            strSql.Append("material=@material,");
            strSql.Append("nums=@nums,");
            strSql.Append("unit=@unit,");
            strSql.Append("suppliername=@suppliername,");
            strSql.Append("supplierphone=@supplierphone,");
            strSql.Append("mstatus=@mstatus,");
            strSql.Append("uid=@uid,");
            strSql.Append("mtype=@mtype,");
            strSql.Append("mbm=@mbm,");
            strSql.Append("mmodel=@mmodel,");
            strSql.Append("did=@did,");
            strSql.Append("cguid=@cguid,");
            strSql.Append("unitPrice=@unitPrice");
            strSql.Append(" where mid=@mid");
            SqlParameter[] parameters = {
					new SqlParameter("@proid", SqlDbType.NVarChar,50),
					new SqlParameter("@usework", SqlDbType.NVarChar,50),
					new SqlParameter("@projectname", SqlDbType.NVarChar,50),
					new SqlParameter("@projecttype", SqlDbType.NVarChar,50),
					new SqlParameter("@supplier", SqlDbType.NVarChar,50),
					new SqlParameter("@material", SqlDbType.NVarChar,250),
					new SqlParameter("@nums", SqlDbType.Decimal,9),
					new SqlParameter("@unit", SqlDbType.NChar,10),
					new SqlParameter("@suppliername", SqlDbType.NVarChar,50),
					new SqlParameter("@supplierphone", SqlDbType.NVarChar,150),
					new SqlParameter("@mstatus", SqlDbType.Int,4),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@mtype", SqlDbType.Int,4),
					new SqlParameter("@mbm", SqlDbType.NVarChar,50),
					new SqlParameter("@mmodel", SqlDbType.NVarChar,200),
					new SqlParameter("@did", SqlDbType.Int,4),
					new SqlParameter("@cguid", SqlDbType.Int,4),
					new SqlParameter("@unitPrice", SqlDbType.Money,8),
					new SqlParameter("@mid", SqlDbType.Int,4)};
            parameters[0].Value = model.proid;
            parameters[1].Value = model.usework;
            parameters[2].Value = model.projectname;
            parameters[3].Value = model.projecttype;
            parameters[4].Value = model.supplier;
            parameters[5].Value = model.material;
            parameters[6].Value = model.nums;
            parameters[7].Value = model.unit;
            parameters[8].Value = model.suppliername;
            parameters[9].Value = model.supplierphone;
            parameters[10].Value = model.mstatus;
            parameters[11].Value = model.uid;
            parameters[12].Value = model.mtype;
            parameters[13].Value = model.mbm;
            parameters[14].Value = model.mmodel;
            parameters[15].Value = model.did;
            parameters[16].Value = model.cguid;
            parameters[17].Value = model.unitPrice;
            parameters[18].Value = model.mid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int mid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from materialinfo ");
			strSql.Append(" where mid=@mid");
			SqlParameter[] parameters = {
					new SqlParameter("@mid", SqlDbType.Int,4)
			};
			parameters[0].Value = mid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
        public bool Deletedid(int mid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from materialinfo ");
            strSql.Append(" where did=@mid");
            SqlParameter[] parameters = {
					new SqlParameter("@mid", SqlDbType.Int,4)
			};
            parameters[0].Value = mid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string midlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from materialinfo ");
			strSql.Append(" where mid in ("+midlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.materialinfo GetModel(int mid)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 mid,proid,usework,projectname,projecttype,supplier,material,nums,unit,suppliername,supplierphone,mstatus,uid,mtype,mbm,mmodel,did,cguid,unitPrice from materialinfo ");
			strSql.Append(" where mid=@mid");
			SqlParameter[] parameters = {
					new SqlParameter("@mid", SqlDbType.Int,4)
			};
			parameters[0].Value = mid;

			lcs.Model.materialinfo model=new lcs.Model.materialinfo();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.materialinfo DataRowToModel(DataRow row)
		{
			lcs.Model.materialinfo model=new lcs.Model.materialinfo();
			if (row != null)
			{
				if(row["mid"]!=null && row["mid"].ToString()!="")
				{
					model.mid=int.Parse(row["mid"].ToString());
				}
				if(row["proid"]!=null)
				{
					model.proid=row["proid"].ToString();
				}
				if(row["usework"]!=null)
				{
					model.usework=row["usework"].ToString();
				}
				if(row["projectname"]!=null)
				{
					model.projectname=row["projectname"].ToString();
				}
				if(row["projecttype"]!=null)
				{
					model.projecttype=row["projecttype"].ToString();
				}
				if(row["supplier"]!=null)
				{
					model.supplier=row["supplier"].ToString();
				}
				if(row["material"]!=null)
				{
					model.material=row["material"].ToString();
				}
				if(row["nums"]!=null && row["nums"].ToString()!="")
				{
					model.nums=decimal.Parse(row["nums"].ToString());
				}
				if(row["unit"]!=null)
				{
					model.unit=row["unit"].ToString();
				}
				if(row["suppliername"]!=null)
				{
					model.suppliername=row["suppliername"].ToString();
				}
				if(row["supplierphone"]!=null)
				{
					model.supplierphone=row["supplierphone"].ToString();
				}
				if(row["mstatus"]!=null && row["mstatus"].ToString()!="")
				{
					model.mstatus=int.Parse(row["mstatus"].ToString());
				}
				if(row["uid"]!=null && row["uid"].ToString()!="")
				{
					model.uid=int.Parse(row["uid"].ToString());
				}
				if(row["mtype"]!=null && row["mtype"].ToString()!="")
				{
					model.mtype=int.Parse(row["mtype"].ToString());
				}
                if (row["mmodel"] != null)
                {
                    model.mmodel = row["mmodel"].ToString();
                }
                if (row["unitPrice"] != null && row["unitPrice"].ToString() != "")
                {
                    model.unitPrice = decimal.Parse(row["unitPrice"].ToString());
                }
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("SELECT [mid],[proid],[usework],[projectname],[projecttype],[supplier],[material],[nums],[unit],[suppliername],[supplierphone],[mstatus],[uid],[user_name],[mtypecn],[mmodel],[unitPrice],[havenums],remark  FROM  [Vmaterialin] where 1=1");
            if(strWhere.Trim()!="")
			{
				strSql.Append(strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" mid,proid,usework,projectname,projecttype,supplier,material,nums,unit,suppliername,supplierphone,mstatus,uid,mtype,unitPrice ");
			strSql.Append(" FROM materialinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM materialinfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
        public int GetCount(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM materialinfo where mstatus=1 and  did="+id);
            
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.mid desc");
			}
			strSql.Append(")AS Row, T.*  from materialinfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "materialinfo";
			parameters[1].Value = "mid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod
            /// <summary>
            /// 根据mid查找supplyPlayId
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
        public string GetRemarkByVTracing(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select remark FROM VTracing where mid=" + id);
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return "";
            }
            else
            {
                return obj.ToString();
            }
        }
        #endregion  ExtensionMethod
    }
}

